/* 
 * File:   Configuration.cpp
 * Author: bd
 * 
 * Created on March 26, 2011, 1:59 PM
 */

#include "Configuration.hpp"
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

Configuration::Configuration() :
sleepDuration_(100), numOfDeliveryActivities_(0), numOfFailoverActivities_(0),
numOfControllerActivities_(0), queueThreshold_(0), importChunkSize_(0),
terminationRetry_(0),maxItemRetry_(0)
{
}

Configuration::Configuration(const Configuration& orig) :
sleepDuration_(orig.sleepDuration_), numOfDeliveryActivities_(orig.numOfDeliveryActivities_),
numOfFailoverActivities_(orig.numOfFailoverActivities_),
numOfControllerActivities_(orig.numOfControllerActivities_),
queueThreshold_(orig.queueThreshold_), importChunkSize_(orig.importChunkSize_),
terminationRetry_(orig.terminationRetry_), maxItemRetry_(orig.maxItemRetry_),
dataSourceConnStr_(orig.dataSourceConnStr_), targetConnStr_(orig.targetConnStr_)
{
}

Configuration::~Configuration()
{
}

//Member functions



//Getter - Setter
void Configuration::logFile(std::string logFile_) {
    this->logFile_ = logFile_;
}
std::string Configuration::logFile() const {
    return logFile_;
}

void Configuration::sleepDuration(int sleepDuration_)
{
    this->sleepDuration_ = sleepDuration_;
}

int Configuration::sleepDuration() const
{
    return sleepDuration_;
}

void Configuration::numOfDeliveryActivities(int numOfDeliveryActivities_)
{
    this->numOfDeliveryActivities_ = numOfDeliveryActivities_;
}

int Configuration::numOfDeliveryActivities() const
{
    return numOfDeliveryActivities_;
}

void Configuration::numOfFailoverActivities(int numOfFailoverActivities_)
{
    this->numOfFailoverActivities_ = numOfFailoverActivities_;
}

int Configuration::numOfFailoverActivities() const
{
    return numOfFailoverActivities_;
}

void Configuration::numOfControllerActivities(int numOfControllerActivities_)
{
    this->numOfControllerActivities_ = numOfControllerActivities_;
}

int Configuration::numOfControllerActivities() const
{
    return numOfControllerActivities_;
}

void Configuration::queueThreshold(int queueThreshold_)
{
    this->queueThreshold_ = queueThreshold_;
}

int Configuration::queueThreshold() const
{
    return queueThreshold_;
}

void Configuration::importChunkSize(int importChunkSize_)
{
    this->importChunkSize_ = importChunkSize_;
}

int Configuration::importChunkSize() const
{
    return importChunkSize_;
}

void Configuration::specialTag(std::string specialTag_)
{
    this->specialTag_ = specialTag_;
}

std::string Configuration::specialTag() const
{
    return specialTag_;
}

void Configuration::dataSourceConnStr(std::string dataSourceConnStr_)
{
    this->dataSourceConnStr_ = dataSourceConnStr_;
}

std::string Configuration::dataSourceConnStr() const
{
    return dataSourceConnStr_;
}

void Configuration::targetConnStr(std::string targetConnStr_)
{
    this->targetConnStr_ = targetConnStr_;
}

std::string Configuration::targetConnStr() const
{
    return targetConnStr_;
}

void Configuration::terminationRetry(int terminationRetry_)
{
    this->terminationRetry_ = terminationRetry_;
}

int Configuration::terminationRetry() const
{
    return terminationRetry_;
}
void Configuration::maxItemRetry(int maxItemRetry_)
{
    this->maxItemRetry_ = maxItemRetry_;
}
int Configuration::maxItemRetry() const
{
    return maxItemRetry_;
}

void load_configuration(const std::string& filename, Configuration& config)
{
    using boost::property_tree::ptree;
    ptree pt;

    read_xml(filename, pt);
    
    config.logFile(pt.get("manifesto.log.log_file", "/tmp/manifesto"));
    config.sleepDuration(pt.get("manifesto.activities.sleep_duration", 1));
    config.numOfDeliveryActivities(pt.get("manifesto.activities.delivery.threads", 1));
    config.numOfFailoverActivities(pt.get("manifesto.activities.failover.threads", 1));
    config.numOfControllerActivities(pt.get("manifesto.activities.controller.threads", 1));
    config.queueThreshold(pt.get("manifesto.activities.controller.queue_threshold", 100));
    config.importChunkSize(pt.get("manifesto.activities.controller.import.chunk_size", 1000));
    config.terminationRetry(pt.get("manifesto.activities.controller.termination_retry", 3));
    config.maxItemRetry(pt.get("manifesto.activities.delivery.max_item_retry", 5));
    config.specialTag(pt.get("manifesto.activities.controller.import.special_tag", "##"));
    config.dataSourceConnStr(pt.get("manifesto.datasource.connection_string", ""));
    config.targetConnStr(pt.get("manifesto.target.connection_string", ""));
}
